/**
 *    Copyright 2010-2016 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.springboot.jpetstore.controller;

import net.sourceforge.stripes.action.SessionScope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springboot.jpetstore.domain.Category;
import org.springboot.jpetstore.domain.Item;
import org.springboot.jpetstore.domain.Product;
import org.springboot.jpetstore.service.CatalogServiceBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author Eduardo Macarron
 *
 */
@Controller
@SessionScope
public class CatalogAction extends AbstractAction {

  private static final long serialVersionUID = 5849523372175050635L;
  public static Logger logger = LoggerFactory.getLogger(CatalogAction.class);

  private static final String MAIN = "/catalog/Main";
  private static final String VIEW_CATEGORY = "/catalog/Category";
  private static final String VIEW_PRODUCT = "/catalog/Product";
  private static final String VIEW_ITEM = "/catalog/Item";
  private static final String SEARCH_PRODUCTS = "/catalog/SearchProducts";

  @Autowired
  private CatalogServiceBean catalogService;


  @RequestMapping("/catalog/main")
  public String viewMain() {
    return MAIN;
  }

  @RequestMapping("/catalog/view")
  public ModelAndView viewCategory(@RequestParam String categoryId) {
    ModelAndView modelAndView = null;
    if (categoryId != null) {
      List<Product> productList = catalogService.getProductListByCategory(categoryId);
      logger.info("CatalogActionBean viewCategory productList-->"+productList.toString());
      Category category = catalogService.getCategory(categoryId);
      logger.info("CatalogActionBean viewCategory category-->"+category.toString());

      modelAndView = new ModelAndView(VIEW_CATEGORY);
      modelAndView.addObject("category",category);
      modelAndView.addObject("productList",productList);
    }
    return modelAndView;
  }

  @RequestMapping("/product/view")
  public ModelAndView viewProduct(@RequestParam String productId){
    ModelAndView modelAndView = new ModelAndView(VIEW_PRODUCT);
    if (productId != null) {
      List<Item> itemList = catalogService.getItemListByProduct(productId);
      Product product = catalogService.getProduct(productId);
      modelAndView.addObject("itemList",itemList);
      modelAndView.addObject("product", product);
      logger.info("CatalogActionBean viewProduct itemList-->"+itemList.toString() + ",/n product:"+product.toString());
    }
    return modelAndView;
  }

  @RequestMapping("/item/view")
  public ModelAndView viewItem(@RequestParam String itemId) {
    ModelAndView modelAndView = new ModelAndView(VIEW_ITEM);
    Item item = catalogService.getItem(itemId);
    Product product = item.getProduct();
    modelAndView.addObject("item",item);
    modelAndView.addObject("product",product);
    return modelAndView;
  }

  @RequestMapping("/product/search")
  public ModelAndView searchProducts(@RequestParam String keyword) {
    ModelAndView modelAndView = new ModelAndView();
    if (keyword == null || keyword.length() < 1) {
      setMessage("Please enter a keyword to search for, then press the search button.");
      modelAndView.setViewName(ERROR);
    } else {
      List<Product> productList = catalogService.searchProductList(keyword.toLowerCase());
      modelAndView.addObject("productList",productList);
      modelAndView.setViewName(SEARCH_PRODUCTS);
    }
    return modelAndView;
  }


}
